// src/controllers/ProductController.ts
import { Inject, Controller, Post, Body, Get, Query, Param } from '@midwayjs/core';
import { ProductService } from '../../service/product/product.service';
import { Products } from '../../entity/products.entity';
import { v4 as uuid } from 'uuid';

@Controller('/api/products')
export class ProductController {
  @Inject()
  productService: ProductService;

  @Post('/create')
  async create(@Body() product: Products): Promise<Products> {
    // 添加 uuid
    product.uuid = uuid();
    return this.productService.create(product);
  }

  @Get('/all')
  async findAll(@Query('pageNo') pageNo: number, @Query('pageSize') pageSize: number): Promise<[Products[], number]> {
    return this.productService.findAll(pageNo, pageSize);
  }

  @Get('/detail/:uuid')
  async findOne(@Param('uuid') uuid: string): Promise<Products> {
    return this.productService.findOne(uuid);
  }

  @Post('/update/:uuid')
  async update(@Param('uuid') uuid: string, @Body() product: Partial<Products>): Promise<Products> {
    return this.productService.update(uuid, product);
  }

  @Post('/remove/:uuid')
  async remove(@Param('uuid') uuid: string): Promise<void> {
    await this.productService.remove(uuid);
  }
}